﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Utils;
using COFE.Core.Validater;

namespace COFE.Core.Profile
{
    public class PropertyDictionary : IPropertyDictionary
    {
        #region Constructor

        #endregion

        #region Methods

        public virtual void RegisterProperty(object property)
        {
            if (!_propertyDic.ContainsValue(property))
                _propertyDic.Add(EnumUtils.GetFullName(property), property);
        }


        public virtual void RegisterProperties<T>()
        {
            foreach (var value in Enum.GetValues(typeof(T)))
                RegisterProperty(value);
        }


        public List<object> GetPropertiesWithDefaultValue()
        {
            return (from value in _propertyDefValueDic.Values
                    where HasDefaultValue(value)
                    select value).ToList();
        }

        public object GetProperty(string propertyString)
        {
            if (_propertyDic.ContainsKey(propertyString))
                return _propertyDic[propertyString];

            foreach (var ppair in _propertyDic)
            {
                string resName = EnumUtils.GetResourceName(ppair.Value, false);
                string alias = AliasExtension.getAlias(ppair.Value);
                if (resName != null && 
                    (
                    propertyString.Equals(resName, StringComparison.InvariantCultureIgnoreCase) ||
                    propertyString.Equals(alias, StringComparison.InvariantCultureIgnoreCase)
                    ))
                    return ppair.Value;
            }

            return null;
        }

        public object GetProperty(int propertyId)
        {
            foreach (var ppair in _propertyDic)
            {
                if ((int)ppair.Value == propertyId)
                    return ppair.Value;
            }
            return null;
        }


        public string GetPropertyString(object property)
        {
            return EnumUtils.GetResourceName(property);
        }

        public bool HasProperty(object property)
        {
            foreach (var ppair in _propertyDic)
                if (ppair.Value.Equals(property))
                    return true;
            return false;
        }

        public bool HasProperty(string propertyString)
        {            
            return GetProperty(propertyString) != null ;
        }

        public bool HasDefaultValue(object property)
        {
            return PropertyTypeExtension.HasDefaultValue(property);
        }

        public object GetDefaultValue(object property)
        {
            return PropertyTypeExtension.GetDefaultValue(property);
        }

        #endregion

        #region Data

        Dictionary<string, object> _propertyDic = new Dictionary<string, object>();
        Dictionary<object, object> _propertyDefValueDic = new Dictionary<object, object>();


        #endregion

        #region Public Properties

        public virtual object[] Properties { get { return _propertyDic.Values.ToArray(); } }

        #endregion




        
    }
}
